package com.ciaojian.client.controller;

import com.ciaojian.client.service.TencentFaceInfoServiceImpl;
import com.ciaojian.core.model.TencentFaceInfo;
import com.ciaojian.core.util.Result;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.iai.v20180301.IaiClient;
import com.tencentcloudapi.iai.v20180301.models.DetectFaceRequest;
import com.tencentcloudapi.iai.v20180301.models.DetectFaceResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author oxiny
 */
@Api(tags = "工具: 腾讯面部识别")
@RestController
@RequestMapping("/tencent-face-info")
public class TencentFaceInfoController {
    @Resource
    private TencentFaceInfoServiceImpl tencentFaceInfoService;

    @ApiOperation("上传脸部照片")
    @PostMapping("/upload")
    public Result upload(Integer userId, String photoUrl) {
        TencentFaceInfo faceInfo = tencentFaceInfoService.lambdaQuery()
                .eq(TencentFaceInfo::getUserId, userId).one();
        if (faceInfo == null) {
            TencentFaceInfo tencentFaceInfo = new TencentFaceInfo()
                    .setUserId(userId)
                    .setPhotoUrl(photoUrl);
            tencentFaceInfo.insert();
        } else {
            faceInfo.setPhotoUrl(photoUrl).updateById();
        }
        return Result.ok();
    }

    private void method(String photoUrl) {
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential("AKID6zcy4t87vSihPq3ze290bx2qNddMoysV", "NTJr88rJUP4SYNvCCvNYS5RVIoRpvkIZ");
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("iai.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            IaiClient client = new IaiClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DetectFaceRequest req = new DetectFaceRequest();
            req.setUrl(photoUrl);
            // 返回的resp是一个DetectFaceResponse的实例，与请求对象对应
            DetectFaceResponse resp = client.DetectFace(req);


//            ObjectMapper om = ObjectMapperFactory.getObjectMapperInstance();
//
//            try {
//                // 输出json格式的字符串回包
//                Map<String, Object> faceInfo =  om.readValue(DetectFaceResponse.toJsonString(resp), Map.class);
//
//            } catch (JsonProcessingException e) {
//                e.printStackTrace();
//            }

        } catch (TencentCloudSDKException e) {
            System.out.println(e);
        }
    }

    @ApiOperation("获取上传的照片")
    @GetMapping
    public Result get(Integer userId) {
        TencentFaceInfo faceInfo = tencentFaceInfoService.lambdaQuery()
                .eq(TencentFaceInfo::getUserId, userId).one();
        return Result.ok(faceInfo);
    }
}
